﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Office = Microsoft.Office.Core;
using MAPI = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Tools.Ribbon;
using System.Drawing;
using Microsoft.Office.Core;
using Sandkeeper.CustomWindows;
using System.Windows.Forms;

// TODO:  Follow these steps to enable the Ribbon (XML) item:

// 1: Copy the following code block into the ThisAddin, ThisWorkbook, or ThisDocument class.

//  protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
//  {
//      return new Ribbon();
//  }

// 2. Create callback methods in the "Ribbon Callbacks" region of this class to handle user
//    actions, such as clicking a button. Note: if you have exported this Ribbon from the Ribbon designer,
//    move your code from the event handlers to the callback methods and modify the code to work with the
//    Ribbon extensibility (RibbonX) programming model.

// 3. Assign attributes to the control tags in the Ribbon XML file to identify the appropriate callback methods in your code.  

// For more information, see the Ribbon XML documentation in the Visual Studio Tools for Office Help.


namespace Sandkeeper
{
    [ComVisible(true)]
    public class Ribbon : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;
        private TimerMainForm timerMain;

        public Ribbon()
        {
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("Sandkeeper.Ribbon.xml");
        }

        #endregion

        /// <summary>
        /// Returns a valid resource image based on the control that calls this function
        /// </summary>
        /// <param name="control">control that needs an image to display</param>
        /// <returns>bitmap image of the resource</returns>
        public Bitmap GetControlImage(IRibbonControl control)
        {
            switch (control.Id)
            {
                case "btnShowTimer":
                    {
                        return new Bitmap(Properties.Resources.K_timer);
                    }
                case "btnSettings":
                    {
                        return new Bitmap(Properties.Resources.Settings);
                    }
                case "btnAbout":
                    {
                        return new Bitmap(Properties.Resources.HourGlass);
                    }
                case "btnHistory":
                    {
                        return new Bitmap(Properties.Resources.History);
                    }
                    
            }
            return null;
        }
        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1
        public void btnHistory_Click(Office.IRibbonControl control)
        {
            History frmHistory = new History();
            frmHistory.ShowDialog();
        }
        /*
        public void btnSettings_Click(Office.IRibbonControl control)
        {
            SettingsForm frmSettings = new SettingsForm();
            frmSettings.ShowDialog();
        }
        */

        public void btnAbout_Click(Office.IRibbonControl control)
        {
            AboutSandKeeperForm frmAbout = new AboutSandKeeperForm();
            frmAbout.ShowDialog();
        }
        public void btnShowTimer_Click(Office.IRibbonControl control)
        {
            MAPI.TaskItem activeTask = null;
                     
            if (timerMain == null || timerMain.IsDisposed)
            {
                // If active folder is not a task folder and there's no active timers then get out of here                   
                if( Globals.ThisAddIn.Application.ActiveExplorer().CurrentFolder.DefaultItemType != MAPI.OlItemType.olTaskItem )
                {
                    MessageBox.Show("Timer can only be called when a task folder is active :(");
                    return;
                }
                try
                {
                    // Make sure only one task is selected.
                    MAPI.Selection selectedItems = Globals.ThisAddIn.Application.ActiveExplorer().Selection;
                    if (selectedItems.Count == 0)
                    {
                        MessageBox.Show("Please select a task to start timing.", "Task needed");
                        return;

                    }
                    if (selectedItems.Count != 1)
                    {
                        MessageBox.Show("Timer can only be started for one task :/", "Too many tasks");
                        return;
                    }
                    activeTask = (MAPI.TaskItem)selectedItems[1];

                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message);
                    return;
                }
                
                timerMain = new TimerMainForm();
                timerMain.ActiveTask = activeTask;
                timerMain.Show();                
            }    
            else
            {                
                timerMain.BringToFront();                
            }
        }

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
        }

        #endregion

        #region Helpers

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }

        #endregion
    }
}
